package com.core.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.system.service.ISystemRoleService;
import com.pojo.dto.PageDto;
import com.pojo.dto.SystemRoleDto;
import com.pojo.req.role.PermissionInsertReq;
import com.utility.gruops.InsertAndUpdateGroups;
import com.utility.gruops.StatusGroup;
import com.utility.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: hw
 * @date: 2020/12/28 17:21
 */
@RestController
@RequestMapping("api/system/role")
public class RoleController {

    @Autowired
    private ISystemRoleService roleService;


    /**
     * 下拉框角色集合
     *      不分页
     * @return
     */
    @GetMapping("/list")
    public Result<List<SystemRoleDto>> selectRoleList(){
        return Result.returnCodeSuccess(roleService.selectRoleList());
    }


    /**
     * 下拉框角色集合
     *      分页
     * @return
     */
    @PostMapping("/page")
    public Result<IPage<SystemRoleDto>> selectRolePage(@RequestBody PageDto pageDto){
        return Result.returnCodeSuccess(roleService.selectRolePage(pageDto));
    }

    /**
     * 下拉框角色集合
     *      分页
     * @return
     */
    @GetMapping("/details")
    public Result selectRoleDetails(SystemRoleDto systemRoleDto){
        return Result.returnCodeSuccess(roleService.selectRoleDetails(systemRoleDto));
    }

    /**
     *    修改角色状态
     * @return
     */
    @PostMapping("/status/update")
    public Result updateStatus(@RequestBody @Validated(value = StatusGroup.class) SystemRoleDto systemRoleDto){
        return Result.returnCodeSuccess(roleService.updateStatus(systemRoleDto));
    }

    /**
     * 新增角色操作权限
     * @return
     */
    @PostMapping("/permission/insert")
    public Result permissionInsert(@RequestBody @Validated PermissionInsertReq permissionInsertReq){
        roleService.updatePermission(permissionInsertReq);
        return Result.returnCodeSuccess();
    }


    /**
     *  查询角色权限
     * @return
     */
    @GetMapping("/permission/list")
    public Result getPermissionList(@RequestParam("roleId")Long roleId){
        return Result.returnCodeSuccess(roleService.getPermissionList(roleId));
    }


    /**
     * 修改角色
     * @param systemRoleDto
     * @return
     */
    @PostMapping("update")
    public Result updateRole(@RequestBody @Validated(value = InsertAndUpdateGroups.class) SystemRoleDto systemRoleDto){
        roleService.updateRole(systemRoleDto);
        return Result.returnCodeSuccess();
    }



    /**
     * 新增角色
     * @param systemRoleDto
     * @return
     */
    @PostMapping("insert")
    public Result insertRole(@RequestBody @Validated(value = InsertAndUpdateGroups.class) SystemRoleDto systemRoleDto){
        roleService.insertRole(systemRoleDto);
        return Result.returnCodeSuccess();
    }



    /**
     * 删除角色
     * @param systemRoleDto
     * @return
     */
    @PostMapping("delete")
    public Result deleteRole(@RequestBody SystemRoleDto systemRoleDto){
        roleService.deleteRole(systemRoleDto);
        return Result.returnCodeSuccess();
    }

    /**
     * 批量删除角色
     * @param systemRoleDto
     * @return
     */
    @PostMapping("batch/delete")
    public Result batchDeleteRole(@RequestBody SystemRoleDto systemRoleDto){
        roleService.batchDeleteRole(systemRoleDto);
        return Result.returnCodeSuccess();
    }

}
